using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dogfight2008.Common;
using System.IO;
using OpenTK.OpenGL;
using OpenTK.Math;
using OpenTK.Input;

namespace Gunner
{
  public partial class MainForm : Form
  {
    readonly double viewdistance = 1000;
    ColoredPolygonMesh airplane = null;
    Vector3 cameraPos = new Vector3(0, -75, 30);
    Vector3 airplanePos = new Vector3(0, 0, 0);
    KeyboardDevice keyboard = new KeyboardDevice();
    double airplanePitch = 0;
    bool LeftDown = false;
    bool RightDown = false;

    public MainForm()
    {
      InitializeComponent();

      airplane = CplLoader.FromByteArray(Mesh.GreenAirplane);
      Application.Idle += new EventHandler(Application_Idle);
      keyboard.KeyDown += new KeyDownEvent(keyboard_KeyDown);
      keyboard.KeyUp += new KeyUpEvent(keyboard_KeyUp);
    }

    void keyboard_KeyUp(KeyboardDevice sender, Key key)
    {
      if (key == Key.Left)
        LeftDown = false;
      if (key == Key.Right)
        RightDown = false;
    }

    void keyboard_KeyDown(KeyboardDevice sender, Key key)
    {
      if (key == Key.Left)
        LeftDown = true;
      if (key == Key.Right)
        RightDown = true;
    }

    void Application_Idle(object sender, EventArgs e)
    {
      double rotvel = 0.03;
      if (LeftDown)
        airplanePitch = (airplanePitch + rotvel) % (2 * Math.PI);
      if (RightDown)
        airplanePitch = (airplanePitch + 2 * Math.PI - rotvel) % (2 * Math.PI);
      float maxx = 40;
      float spd = 0.3f;
      Vector3 airplaneVel = spd*new Vector3((float)Math.Cos(airplanePitch), 0, (float)Math.Sin(airplanePitch));
      airplanePos += airplaneVel;
      if (airplanePos.X > maxx)
        airplanePos.X -= 2 * maxx;
      if (airplanePos.X < -maxx)
        airplanePos.X += 2 * maxx;

      glControl1.Invalidate();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
      GLControlUtil.Init(glControl1, viewdistance);
      GLUtil.SunLightDirection(0, 0, -1);
      glControl1.Focus();
    }

    private void glControl_Paint(object sender, PaintEventArgs e)
    {
      GLUtil.Clear();
      //if (LeftDown)
      //  GL.Translate(10, 0, 0);
      //if (RightDown)
      //  GL.Translate(0, 0, 10);
      GL.Translate(-cameraPos);
      GLUtil.DrawXYZAxis(200);
      GL.Translate(airplanePos);
      GL.Rotate((float)(-airplanePitch / (Math.PI * 2) * 360.0), Vector3.UnitY);
      GL.Rotate(-90, Vector3.UnitZ);
      GLUtil.DrawMesh(airplane);
      GLControlUtil.SwapBuffers(glControl1);
    }

    private void glControl_Resize(object sender, EventArgs e)
    {
      GLControlUtil.HandleResize(glControl1, viewdistance);
    }

    private void glControl1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.I)
        LeftDown = true;
      if (e.KeyCode == Keys.O)
        RightDown = true;
    }

    private void glControl1_KeyUp(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.I)
        LeftDown = false;
      if (e.KeyCode == Keys.O)
        RightDown = false;
    }
  }
}